//
//  YDFMDBDatabaseQueue.m
//  YDBASE
//
//  Created by wangyd on 2021/12/24.
//  Copyright © 2021 wuyezhiguhun. All rights reserved.
//

#import <FMDB/FMDB.h>

#import "YDFMDBDatabaseQueue.h"
#import "YDUserInfo.h"

@interface YDFMDBDatabaseQueue()

@property (nonatomic, copy) NSString *databasePath;

@property (nonatomic, strong) FMDatabaseQueue *databaseQueue;

@end


@implementation YDFMDBDatabaseQueue


- (void)insertFMDBDatabaseList {
    
    FMDatabaseQueue *databaseQueue = [FMDatabaseQueue databaseQueueWithPath:self.databasePath];
    //串行
    dispatch_queue_t dispatchQueue1 = dispatch_queue_create("queue1", DISPATCH_QUEUE_SERIAL);
    //并行
    dispatch_queue_t dispatchQueue2 = dispatch_queue_create("queue2", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(dispatchQueue1, ^{
        
        for (int i = 0; i < 100; i++) {
            [databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
                
                NSInteger userId = arc4random() % 100000 + 1;
                NSString *sql = [NSString stringWithFormat:@"insert into user(user_id,user_name,user_age) values('%05ld','%@','%@')", userId, [[YDUserInfo sharedUserInfo] getUserName], [[YDUserInfo sharedUserInfo] getUserAge]];
                
                BOOL res = [db executeUpdate:sql];
                
                NSLog(@"wuyezhiguhun =>: YDFMDBDatabaseQueue =>: currentThread =>: %@", [NSThread currentThread]);
                
                if (res) {
                    NSLog(@"wuyezhiguhun =>: YDFMDBDatabaseQueue =>: 数据库插入 =>: %d =>: 成功", i);
                } else {
                    NSLog(@"wuyezhiguhun =>: YDFMDBDatabaseQueue =>: 数据库插入 =>: 失败");
                }
            }];
        }
        
    });
    
    dispatch_async(dispatchQueue2, ^{
        
        for (int i = 100; i < 200; i++) {
            [databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
                
                NSInteger userId = arc4random() % 100000 + 1;
                NSString *sql = [NSString stringWithFormat:@"insert into user(user_id,user_name,user_age) values('%05ld','%@','%@')", userId, [[YDUserInfo sharedUserInfo] getUserName], [[YDUserInfo sharedUserInfo] getUserAge]];
                
                BOOL res = [db executeUpdate:sql];
                
                NSLog(@"wuyezhiguhun =>: YDFMDBDatabaseQueue =>: currentThread =>: %@", [NSThread currentThread]);
                
                if (res) {
                    NSLog(@"wuyezhiguhun =>: YDFMDBDatabaseQueue =>: 数据库插入 =>: %d =>: 成功", i);
                } else {
                    NSLog(@"wuyezhiguhun =>: YDFMDBDatabaseQueue =>: 数据库插入 =>: 失败");
                }
            }];
        }
        
    });
}


- (NSString *)databasePath {
    if (!_databasePath) {
        //获得数据库文件的路径
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        _databasePath = [documentPath stringByAppendingPathComponent:@"FMDBDatabase.sqlite"];
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据库地址 =>: %@", _databasePath);
    }
    return _databasePath;
}

- (FMDatabaseQueue *)databaseQueue {
    if (!_databaseQueue) {
        _databaseQueue = [FMDatabaseQueue databaseQueueWithPath:self.databasePath];
    }
    return _databaseQueue;
}

@end
